//
//  SY_Tch_OrdersViewController.m
//  ChatDemo-UI2.0
//
//  Created by Sylar on 2016/12/20.
//  Copyright © 2016年 Sylar. All rights reserved.
//

#import "SY_Tch_OrdersViewController.h"
#import "SY_Tch_OrderCell.h"
#import "SY_Par_PaymentModel.h"
#import "SY_PaymentCountViewController.h"

@interface SY_Tch_OrdersViewController ()<UITableViewDelegate,UITableViewDataSource,reloadDataDelegate>
{
    NSInteger pageNum;
    
    EmptyStateView *emptyView;//空状态
    EmptyStateView *noWifiView;//无网络状态
}
@property (nonatomic, strong) UITableView *ordersTable;
@property (nonatomic, strong) NSMutableArray *ordersArr;

@end

@implementation SY_Tch_OrdersViewController

- (void)loadView {
    [super loadView];
    
    self.navBar.nav_titleLabel.text = @"校园收费";
    
    pageNum = 1;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //Do any additional setup after loading the view.
    
    [self loadOrdersRequest];
    [self.view addSubview:self.ordersTable];
    
    emptyView = [[EmptyStateView alloc] initWithFrame:CGRectMake((SCREENWIDTH-120)/2, (SCREENHEIGH-110)/2-70, 120, 110) andBackImage:@"list_empty" andBottomStr:@"暂无收费记录"];
    [self.view addSubview:emptyView];
    
    noWifiView = [[EmptyStateView alloc] initNoWifiWithFrame:CGRectMake(0, (SCREENHEIGH-64-175)/2, SCREENWIDTH, 175) andBackImage:@"noWifi" andBottomStr:@"网络出错啦,点击重新加载"];
    noWifiView.userInteractionEnabled= YES;
    noWifiView.delegate = self;
    [self.view addSubview:noWifiView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    //Dispose of any resources that can be recreated.
}

#pragma mark - Method



#pragma mark - Request

- (void)loadOrdersRequest {
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:@"api_receiptlist_toteacher" forKey:@"method"];
    [param setObject:USERID forKey:@"userid"];
    [param setObject:@"1" forKey:@"pageno"];
    [param setObject:@"20" forKey:@"pagesize"];
    
    [Utils noIndiGet:nil parameters:param success:^(NSInteger recode, id jsonObject){
        
        NSArray *dataArr = [jsonObject objectForKey:@"re_data"];
        
        if (dataArr && dataArr.count > 0) {
            
            self.ordersArr = [[NSMutableArray alloc]init];
            
            for (NSDictionary *dic in dataArr) {
                
                SY_Par_PaymentModel *model = [SY_Par_PaymentModel mj_objectWithKeyValues:dic];
                
                [self.ordersArr addObject:model];
                
            }
        }
        
        [self.ordersTable.mj_header endRefreshing];
        [self.ordersTable reloadData];
        
        if (self.ordersArr.count == 0) {
            [self->noWifiView hideEmptyImage];
            [self->emptyView showEmptyImage];
        }
        
        
    }failure:^(NSInteger recode, NSError *error) {
        if (error.code == -1009) {
            
            self.ordersTable.hidden = YES;
            [self.ordersTable.mj_header endRefreshing];
            [self->emptyView hideEmptyImage];
            [self->noWifiView showEmptyImage];
            
        }
    }];
    
}

- (void)loadMoreOrdersRequest {
    
    pageNum += 1;
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:@"api_receiptlist_toteacher" forKey:@"method"];
    [param setObject:USERID forKey:@"userid"];
    [param setObject:[NSString stringWithFormat:@"%ld",pageNum] forKey:@"pageno"];
    [param setObject:@"20" forKey:@"pagesize"];
    
    [Utils noIndiGet:nil parameters:param success:^(NSInteger recode, id jsonObject){
        
        
        NSArray *dataArr = [jsonObject objectForKey:@"re_data"];
        
        if (dataArr.count > 0) {
            for (NSDictionary *dic in dataArr) {
                
                SY_Par_PaymentModel *model = [SY_Par_PaymentModel mj_objectWithKeyValues:dic];
                
                [self.ordersArr addObject:model];
            }
            
            [self.ordersTable.mj_footer endRefreshing];
            [self.ordersTable reloadData];
            
        }else {
            self->pageNum -= 1;
            [self.ordersTable.mj_footer endRefreshingWithNoMoreData];
        }
        
        
        
    }failure:^(NSInteger recode, NSError *error) {
        self->pageNum -= 1;
    }];
}

#pragma mark - reloadDataDelegate

- (void)reloadCurrentData {
    
    [noWifiView hideEmptyImage];
    self.ordersTable.hidden = NO;
    
    [self loadOrdersRequest];
}

#pragma mark - TableView

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [self.ordersArr count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SY_Tch_OrderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];
    
    if (cell == nil) {
        cell = [[SY_Tch_OrderCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cellIdentifier"];
    }
    
    [cell creatCell:[self.ordersArr objectAtIndex:indexPath.row]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SY_PaymentCountViewController *countVC = [[SY_PaymentCountViewController alloc]init];
    countVC.detailModel = [self.ordersArr objectAtIndex:indexPath.row];
    
    [self.navigationController pushViewController:countVC animated:YES];
    
}

#pragma mark - Get

- (UITableView *)ordersTable {
    
    if (_ordersTable == nil) {
        
        _ordersTable = [[UITableView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREENWIDTH, SCREENHEIGH-SafeAreaTopHeight)];
        _ordersTable.delegate = self;
        _ordersTable.dataSource = self;
        _ordersTable.rowHeight = 96;
        _ordersTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        _ordersTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [self loadOrdersRequest];
        }];
        
        _ordersTable.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            
            [self loadMoreOrdersRequest];
            
        }];
        
    }
    
    return _ordersTable;
}


@end
